package com.base.controller;

import com.base.po.novice.NoviceClass;
import com.base.service.NoviceClazzService;
import com.base.service.UsersService;
import com.base.utils.Produce;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 新手课堂
 * 
 * @author f
 * 
 */
@Controller
@RequestMapping("/admin")
public class index_NoviceList {

	@Autowired
	private NoviceClazzService noviceClazzService; // 每日任务信息
	@Autowired
	private UsersService usersService;

	// WEB端进入新手指南页面
	@RequestMapping(value = "/noviceListIndex")
	public ModelAndView noviceListIndex(HttpServletRequest request) {
		String userid = (String) request.getSession().getAttribute("adminUserId"); // 登录的用户ID
		System.out.println("进入"+userid);
		if (userid == null){
			return new ModelAndView("jsp/loginCheck"); // 未登录
		}else{
			System.out.println("返回视图");
			//List<NoviceClass> nc = noviceClazzService.findAllNoviceClassInfo(0,Integer.MAX_VALUE);
			List<NoviceClass> nc = noviceClazzService.findAllNoviceClass(userid, 1);
			System.out.println(nc);
			return new ModelAndView("jsp/index_NoviceList", "noviceList",nc );
		}

	}

	//跳转到修改页面
	@RequestMapping(value = "/toUpdateNoviceList")
	public ModelAndView updateNoviceList(HttpServletRequest request,String ncId) {
		String userid = (String) request.getSession().getAttribute("adminUserId"); // 登录的用户ID
		System.out.println("进入修改"+userid);
		if (userid == null){
			return new ModelAndView("jsp/loginCheck"); // 未登录
		}else{
			System.out.println("返回视图");
			NoviceClass nc = noviceClazzService.findNoviceClassById(ncId);
			System.out.println(nc);
			return new ModelAndView("jsp/update_NoviceList", "nc",nc );
		}

	}

	/**
	 * 修改新手指南信息
	 * @param nc
	 * @return
	 */
	@RequestMapping(value = "/updateNoviceList",method = RequestMethod.POST)
	@ResponseBody
	public int updateNoviceList(HttpServletRequest request,NoviceClass nc){
		System.out.println("修改中的"+nc);
		nc.setNcDatastatus(Byte.valueOf("1"));
		String userid = (String) request.getSession().getAttribute("adminUserId"); // 登录的用户ID
		nc.setNcParent(userid);
		if (request.getSession().getAttribute("adminUserId") == null){
			return -1; // 未登录
		}else{
			return noviceClazzService.updateNoviceClassByNcId(nc);
		}
	}

	/**
	 * 更改新手指南数据状态
	 * @param
	 * @param
	 * @return
	 */
	@RequestMapping(value = "updateNoviceDataStatus",method = RequestMethod.POST)
	@ResponseBody
	public int updateNoviceDataStatus(HttpServletRequest request,String ncId,int ncDatastatus){
		if (request.getSession().getAttribute("adminUserId") == null){
			return -1; // 未登录
		}else{
			if (noviceClazzService.updateNoviceDataStatusById(ncId,ncDatastatus)){
				return 1;
			}else{
				return 0;
			}
		}
	}

	// 跳转添加每日任务
	@RequestMapping("/addNoviceList")
	public String addTaskList() throws Exception {
		return "jsp/add_NoviceList";
	}

	/**
	 * 添加新手指南
	 * @param request
	 * @param noviceClass
	 * @return
	 */
	@RequestMapping(value = "/addTNoviceList", method = RequestMethod.POST)
	public @ResponseBody int toAddTaskList(HttpServletRequest request, NoviceClass noviceClass){
		System.out.println("进入添加");
		System.out.println(noviceClass);
		String userid = (String) request.getSession().getAttribute("adminUserId"); // 登录的用户ID
		if (userid == null)
			return -1; // 未登录
		//List<TaskList> btl = taskListService.selectByAll(new TaskList(taskList.getType(), userid)); // 任务信息
		//List<NoviceClass> ncs = noviceClazzService.adminFindAllInfo();
		noviceClass.setNcCreatetime((new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(new Date()));
		noviceClass.setNcParent(userid);
		if (noviceClazzService.adminIudgeNoviceInfo(noviceClass)){
			System.out.println("以添加");
			return -2; // 已添加该任务
		}else{
			noviceClass.setNcDatastatus(Byte.valueOf("1"));
			noviceClass.setNcId(Produce.getUUID()); // 设置参数 UUID
			System.out.println("未添加");
			return noviceClazzService.insertNoviceClassByNcParent(noviceClass);
		}
	}
}
